package com.yangjian.web;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yangjian.bean.Employee;
import com.yangjian.comon.R;
import com.yangjian.page.PageParameter;
import com.yangjian.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Transactional
@RestController
@Api(tags = "登录相关接口和员工相关接口")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService em;

    //@RequestBody Employee employee  ：指的是将json字符串转化为javabean对象，用这个对象去接受数据

    @ApiOperation("登录接口")
    @PostMapping("/employee/login")
    public R employeeController(@RequestBody Employee employee, HttpSession session){

        //调用service
        Employee login = em.login(employee);

        if (login !=null){//登录成功
            //将数据保存到session作用域中
            session.setAttribute("employee",login);
            return R.success(login);
        }else {//登录失败
            return R.error("账号或者密码错误~");
        }


    }

    @ApiOperation("退出接口")
    @PostMapping("/employee/logout")
    public R logout(HttpSession session){

        //将存在session的数据给干掉
        session.invalidate();
        //给页面一个提示
        return R.success("退出成功！");

    }

    @ApiOperation("新增员工接口")
    @PostMapping("/employee")
    public R add(@RequestBody Employee employee, HttpSession session){

        //补充员工信息
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes())); //设置初始密码
        employee.setStatus(1);      //设置初始状态
        //employee.setCreateTime(LocalDateTime.now());            //设置创建的时间
        //employee.setUpdateTime(LocalDateTime.now());            //设置修改的时间
        ////从session作用域中取是谁设置的
        //Employee employee1= (Employee) session.getAttribute("employee");
        //employee.setCreateUser(employee1.getId());      //获取设置人的id
        //employee.setUpdateUser(employee1.getId());      //获取修改人的id；

        //调用service方法
        int addRow = this.em.add(employee);
        log.debug("添加成功返回的结果{}",addRow);

        if (addRow>0){

            return R.success("添加员工成功");
        }else {
            return R.error("添加员工失败");
        }

    }


    /**
     * 分页查询
     * @param pageParameter
     * @return
     */
    @ApiOperation("分页查询接口")
    @GetMapping("/employee/page")
    public R findPage(PageParameter pageParameter){

        IPage<Employee> page = em.findPage(pageParameter);

        System.out.println("page = " + page);
        if (page !=null){

            return R.success(page);
        }else {
            return R.error("分页查询失败");
        }

    }


    /**
     * 修改员工信息
     * @param employee
     * @param session
     * @return
     */
    @ApiOperation("修改员工状态接口")
    @PutMapping("/employee")
    public R update(@RequestBody Employee employee,HttpSession session){
        //补充数据
       //Employee employee1 = (Employee) session.getAttribute("employee");
       //employee.setUpdateTime(LocalDateTime.now());
       //employee.setUpdateUser(employee1.getId());
        //调用service
        int row = em.update(employee);
        //响应页面

        if (row>0){

            return R.success("修改员工成功");
        }else {
            return R.error("修改员工失败");
        }

    }

}
